package com.lanou3g.springcloud.calc.provider.web;

import com.lanou3g.springcloud.calc.provider.bean.CalcResult;
import com.lanou3g.springcloud.calc.provider.bean.SystemConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
public class CalcController {

	@RequestMapping("/calc/{numA}/{numB}/{operator}")
	public CalcResult calc(@PathVariable int numA, @PathVariable int numB, @PathVariable int operator) {
		Integer result = null;
		switch (operator) {
			case SystemConstants
					.OPERATOR_ADD:
				result = numA + numB;
				break;
			case SystemConstants
					.OPERATOR_MINUS:
				result = numA - numB;
				break;
			case SystemConstants
					.OPERATOR_MULTIPLY:
				result = numA * numB;
				break;
			case SystemConstants
					.OPERATOR_DIVISION:
				result = numA / numB;
				break;
			default:
				log.error("不支持的运算符： " + operator);
				break;
		}

		CalcResult calcResult = new CalcResult();
		calcResult.setNumA(numA);
		calcResult.setNumB(numB);
		calcResult.setOperator(getOperatorStr(operator));
		calcResult.setResult(result);
		return calcResult;
	}

	private String getOperatorStr(int operator) {
		String ret = "不支持的运算符";
		switch (operator) {
			case SystemConstants
					.OPERATOR_ADD:
				ret = "+";
				break;
			case SystemConstants
					.OPERATOR_MINUS:
				ret = "-";
				break;
			case SystemConstants
					.OPERATOR_MULTIPLY:
				ret = "×";
				break;
			case SystemConstants
					.OPERATOR_DIVISION:
				ret = "÷";
				break;
			default:
				log.error("不支持的运算符： " + operator);
				break;
		}
		return ret;
	}
}
